package ochart.elements.axes
{
	import flash.display.Sprite;
	import flash.text.TextField;
	
	import ochart.core.XYChart;
	import ochart.elements.labels.Label;
	import ochart.utils.ArrayUtil;
	
	public class XAxis extends AxisBase
	{
		public static const CLASS_NAME:String = "XAxis"
		
		override public function get className():String
		{
			return CLASS_NAME;
		}
		
		public static const ALIAS:String = "xAxis";
		
		override public function get alias():String
		{
			return ALIAS;
		}
		
		private var defaultValue:Object = 
		{
			
		}
			
		private var _propertiesArray:Array = null;
		
		override public function get propertiesArray():Array
		{
			if(_propertiesArray)
			{
				return _propertiesArray;
			}
			
			_propertiesArray = super.propertiesArray;
			for(var name:String in defaultValue)
			{
				if(!ArrayUtil.containsValue(_propertiesArray, name))
				{
					_propertiesArray.push(name);
				}
			}
			return _propertiesArray;
		}
		
		public function XAxis()
		{
			super();
		}
		
		override public function getDefaultValue(name:String):*
		{
			return (defaultValue[name] === undefined) ? super.getDefaultValue(name) : defaultValue[name];
		}
		
		override public function update(callAfterUpdate:Boolean=true):Boolean
		{
			if(!super.update(false))
			{
				return false;
			}
			
			if(isTicksChange || isLabelsChange)
			{
				var bottom:Number = (serializationValue["bottom"]) ? serializationValue["bottom"] : 0;
				
				isTicksChange = false;
				var maxTickHeight:Number = 0;
				var tickIndex:int = 0;
				for each(var tick:Tick in ticksArray)
				{
					var tickX:Number = (tickIndex == ticksArray.length - 1) ? (stepLength * (max - min)) : (stepLength * steps * tickIndex);
					tick.x = offset + barOffset + tickX - tick.centerX;
					tick.y = height;
					if(maxTickHeight < tick.totalHeight)
					{
						maxTickHeight = tick.totalHeight;
					}
					tickIndex++;
				}
				bottom += maxTickHeight;
				
				isLabelsChange = false;
				var maxLabelHeight:Number = 0;
				var labelIndex:int = 0;
				for each(var label:Label in labelsArray)
				{
					if(label)
					{
						var relatedTick:Tick = null;
						if(labelIndex >= 0 && ticksArray.length > labelIndex)
						{
							relatedTick = ticksArray[labelIndex];
						}
						var labelX:Number = (labelIndex == labelsArray.length - 1) ? (stepLength * (max - min)) : (stepLength * steps * labelIndex);
						label.x = offset + barOffset + labelX - label.centerX;
						label.y = (relatedTick) ? (relatedTick.y + relatedTick.maxHeight + relatedTick.bottom) : height;
						if(maxLabelHeight < label.totalHeight)
						{
							maxLabelHeight = label.totalHeight;
						}
					}
					labelIndex++;
				}
				bottom += maxLabelHeight;
				
				this.bottom = bottom;
			}
			
			if(callAfterUpdate)
			{
				return afterUpdate();
			}
			return true;
		}
		
		override public function calculate():void
		{
			super.calculate();
			
			stepLength = (width - (offset * 2) - (barOffset * 2)) / (max - min);
			
//			blockWidth =  (width - (offset * 2) - (barOffset * 2)) / (Math.ceil((max - min) / steps) + ((chart as XYChart).hasBar ? 2 : 1));
		}
	}
}